package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.poi.PoiAddRequest;
import com.example.fireassistant_server.dto.poi.PoiUpdateRequest;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.mapper.PoiMapper;
import com.example.fireassistant_server.service.PoiService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.PoiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/poi")
public class PoiController {

    UserService userService;

    PoiService poiService;

    WechatUserService wechatUserService;

    PoiMapper poiMapper;

    // 添加poi点 ；普通用户可以添加
    @PostMapping("/add/{user_id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    PoiVo Add(@Validated @RequestBody PoiAddRequest poiAddRequest, @PathVariable String user_id){
        User user = userService.get(user_id);
        return poiMapper.toVo(poiService.addPoi(poiAddRequest, user));
    }
    // 微信端添加poi点；普通用户可以添加
    @PostMapping("/weChat/add")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    PoiVo weAdd(@Validated @RequestBody PoiAddRequest poiAddRequest, @RequestHeader(value = "X-WX-OPENID") String openId){
        User user = wechatUserService.getCurrentUser(openId).getUser();
        return poiMapper.toVo(poiService.addPoi(poiAddRequest,user));
    }

    //根据id获取某个Poi点
    @GetMapping("/{id}")
    PoiVo get(@PathVariable String id){
        System.out.println(poiService.get(id));
        return poiMapper.toVo(poiService.get(id));
    }

    //根据id来更新Poi点
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    PoiVo update(@PathVariable String id,
                         @Validated @RequestBody PoiUpdateRequest poiUpdateRequest) {
        return poiMapper.toVo(poiService.update(id,poiUpdateRequest));
    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    void delete(@PathVariable String id) {
        poiService.delete(id);
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setPoiService(PoiService poiService) {
        this.poiService = poiService;
    }

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setPoiMapper(PoiMapper poiMapper) {
        this.poiMapper = poiMapper;
    }
}
